<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/test/image/', function (\Illuminate\Http\Request $request) {
    $http = \App\Services\Http::getInstance([
        'headers' => [
            'host' => 'mmbiz.qpic.cn'
        ]
    ]);
    $url = 'https://mmbiz.qpic.cn'.$request->get('path').'';
    $response = $http->get($url, []);
    return response($response->getBody()->getContents())->header(
        'Content-type', 'image/webp'
    );
});

Route::get('/test/article', function (\Illuminate\Http\Request $request){
    $url = 'https://mp.weixin.qq.com/s?__biz=MzAxNTEzNDUxNA==&mid=2652874886&idx=2&sn=58dae3971089b09d33a5f16f6654b97f&chksm=8063b2dbb7143bcda0bff986a795694502c97c884af4e4b6c1e9e76363e8ff320642a999feca#rd';
    $http = \App\Services\Http::getInstance();
    $response = $http->get($url);
    $contents = $response->getBody()->getContents();

    $contents = preg_replace(
        '/data-src="https:\/\/mmbiz.qpic.cn(\/mmbiz_jpg\/.*?\/640\?wx_fmt=jpeg)"/',
        'data-src="http://'.$_SERVER['HTTP_HOST'].'/test/image/?path=${1}"',
        $contents
    );

    $contents = preg_replace('/<p style="white-space: normal;text-align: center;">.*?<\/p>/', '', $contents, 1);
    $contents = preg_replace('/<p style="white-space: normal;max-width: 100%;min-height: 1em;.*?<\/p>/', '', $contents);

    return $contents;
});

